package com.itheima;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 2025/5/16 19:44
 */
public class ArraysDemo {
    public static void main(String[] args) {
        Student s1 = new Student("张三", 18);
        Student s2 = new Student("李四", 19);
        Student s3 = new Student("王五", 20);
        Student s4 = new Student("赵六", 21);

        /*Student[] arr = new Student[4];
        arr[0] = s1;
        arr[1] = s2;
        arr[2] = s3;
        arr[3] = s4;*/

        Student[] arr = {s1,s2,s3,s4};
        //打印数组
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

        //使用Arrays工具类对数组进行排序
        /*Arrays.sort(arr, new Comparator<Student>() {
            *//**
             *
             * @param o1 the first object to be compared.
             * @param o2 the second object to be compared.
             * @return 如果返回0，则代表两个对象一样大，如果返回1，则代表o1大，如果返回-1，则代表o2大
             * 如果是按照正序排序，则需要让左侧的数据-右侧的数据
             * 如果是按照倒序排序，则需要让右侧的数据-左侧的数据
             *//*
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge() - o1.getAge();
            }
        });*/

        Arrays.sort(arr, (o1, o2) -> o2.getAge() - o1.getAge());

        System.out.println("排序后");
        //打印数组
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

    }
}
